import os

from dotenv import load_dotenv
from fastapi import FastAPI,Depends
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from .dependencies import oauth2_scheme

from .routers import router
from fastapi.responses import StreamingResponse
from .config import tags_metadata
from fastapi.middleware.gzip import GZipMiddleware

description = """
## 软件杯比赛项目API文档

### 主要功能
- 多模态输入
- 资源管理
- 资源制作

| 资源类型   |
|------------|
|textbook   |
| paper      |
| voice      |
| ppt        |
| image      |
| video      |
[👉 点击访问项目官网](https://fastapi.tiangolo.com)
"""
version="1.0.0"
terms_of_service="http://example.com/terms/"
contact={
    "name": "gd",
    "url": "https://fastapi.tiangolo.com",
    "email": "qianche_1225@qq.com",
}
license={
    "name": "Apache 2.0",
    "url": "https://www.apache.org/licenses/LICENSE-2.0.html",
}

app = FastAPI(
    title="什么队软件杯",
    description=description,
    version="1.0.0",
    terms_of_service=terms_of_service,
    contact=contact,license=license,
    # openapi_url="/docs/v1/docs.json",
    redoc_url="/ddd",
    docs_url="/docs",
    openapi_tags=tags_metadata,
    # debug=False,
  )
# app.add_middleware(GZipMiddleware, minimum_size=1000)
app.include_router(router)

# 确保正确加载 .env 文件
# env_path = os.path.join(os.path.dirname(__file__), '.env')
# load_dotenv(env_path)  # 或直接使用 load_dotenv() 如果 .env 在项目根目录

load_dotenv()
SOURCES_PATH=os.getenv("SOURCES_PATH")
sources_path=os.path.join(SOURCES_PATH,"sources")
app.mount("/sources", StaticFiles(directory=sources_path), name="sources")

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)
@app.get("/")
async def root():
    return {"message": "Hello World"}


@app.get("/hello/{name}")
async def say_hello(name: str,token: str = Depends(oauth2_scheme)):
    return {"message": f"Hello {name}"}


from fastapi.responses import FileResponse


@app.get("/images/a.png")
async def get_image():
    image_path = r"D:\aaaaazzz\union_project\union_project1\img.png"

    def iterfile():
        with open(image_path, mode="rb") as file:
            yield from file

    return StreamingResponse(iterfile(), media_type="image/png")

@app.get("/images/b.png")
async def get_image():
    image_path = r"D:\aaaaazzz\union_project\union_project1\img.png"
    from utils.photo_tools import get_image_as_base64
    image=get_image_as_base64(image_path)
    return {
        "image":image
    }

if __name__ == "__main__":
    import uvicorn
    # uvicorn.run(app, host="0.0.0.0", port=8000)
    print(os.getenv("DATABASE_URL"))